﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using ScrumTable.Common;
using ScrumTable.Common.Monitoring;
using ScrumTable.Common.Security;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.TFS.Services.FileSystem;
using ScrumTable.DL.Data.TFS.Services.Reporting;
using ScrumTable.DL.Data.TFS.Services.TeamFoundation;

#endregion

namespace ScrumTable.DL.Data.TFS.Bulk.Services
{
    /// <summary>
    /// This class is used to load all data below a given project.
    /// </summary>
    internal partial class InitialDataCacheLoader
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        partial void StartLoadMemberData(DataLoadContext context)
        {
            try { LoadMemberData(context); }
            catch (Exception e) { _importMonitor.EndSubProgress(MemberLoadPart, e); throw; }
        }

        private void LoadMemberData(DataLoadContext context)
        {
            _importMonitor.StartSubProgress(MemberLoadPart);

            int memberIdx = 0;
            foreach (CacheMember member in context.CacheProject.Members)
            {
                member.PictureUrl = context.TfsContext.GetTeamIdentityImageUri(member.BackEndId);
                _importMonitor.ReportProgress(MemberLoadPart, ++memberIdx, context.CacheProject.Members.Count);
            }
            _importMonitor.EndSubProgress(MemberLoadPart);
        }

        private DataLoadContext CreateDataLoadContext(CacheProject project, TfsServerWrapper tfsInstance)
        {
            return new DataLoadContext
            {
                TfsContext = tfsInstance,
                FileSystemContext = new FsContext(),
                ReportingContext = new RsContext(project.ReportingPortal, _connectionCtx.Selected),

                CacheProject = project,
                TfsProject = tfsInstance.WorkItems.Projects[project.Name]
            };
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion


        //--------------------------------------------------------------------
        // Nested Classes
        //--------------------------------------------------------------------

        internal class DataLoadContext : DisposeBase
        {
            internal IList<CacheReport> LoadedReports { get; private set; }
            internal IList<CacheIteration> LoadedIterations { get; private set; }
            internal Dictionary<int, CacheWorkItem> LoadedWorkItems { get; private set; }

            internal CacheProject CacheProject { get; set; }
            internal Project TfsProject { get; set; }

            internal TfsServerWrapper TfsContext { get; set; }
            internal RsContext ReportingContext { get; set; }
            internal FsContext FileSystemContext { get; set; }
            internal CacheContext CacheContext { get { return CacheProject.OwnerContext; } }

            internal DataLoadContext()
            {
                LoadedReports = new List<CacheReport>();
                LoadedIterations = new List<CacheIteration>();
                LoadedWorkItems = new Dictionary<int, CacheWorkItem>();
            }

            /// <summary>
            /// Releases managed (c# or .net) resources.
            /// </summary>
            protected override void ReleaseManagedResources()
            {
                if (TfsContext != null)
                {
                    TfsContext.Dispose();
                }
            }
        }
    }
}